package org.csu.mypetstoreplus.service;

import org.csu.mypetstoreplus.entity.*;
import org.csu.mypetstoreplus.persistence.*;
import org.csu.mypetstoreplus.util.ThreadLocalUtil;
import org.csu.mypetstoreplus.vo.ItemVO;
import org.csu.mypetstoreplus.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class OrderService
{
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderStatusDao orderStatusDao;
    @Autowired
    private LineItemDao lineItemDao;
    @Autowired
    private CartItemDao cartItemDao;
    @Autowired
    private InventoryDao inventoryDao;
    @Autowired
    private SequenceDao sequenceDao;

    public void newOrder(OrderVO orderVO) {
        //获取当前用户名
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");

        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        orderVO.setOrderId(getNextId("ordernum") - 1);
        orderVO.setOrderDate(sdf.format(date));
        orderVO.setUsername(username);
        orderVO.setStatus("P");

        List<LineItem> lineItems = orderVO.getLineItems();

        int index = 0;
        for(LineItem lineItem : lineItems) {
            Inventory inventory = inventoryDao.findByItemId(lineItem.getItemId());
            inventory.setQuantity(inventory.getQuantity() - lineItem.getQuantity());
            inventoryDao.save(inventory);

            index++;
            lineItem.setLineNum(index);
            lineItem.setOrderId(orderVO.getOrderId());
            lineItemDao.save(lineItem);
            cartItemDao.actuallyDeleteByUsername(username, lineItem.getItemId());
        }

        Order order = new Order(orderVO);
        OrderStatus orderStatus = new OrderStatus(orderVO);
        orderDao.save(order);
        orderStatusDao.save(orderStatus);
    }

    public OrderVO getOrder(int orderId) {
        Order order = orderDao.findByOrderId(orderId);
        if (order == null) {
            return null; // Order not found
        }

        OrderStatus orderStatus = orderStatusDao.findByOrderId(orderId);
        List<LineItem> lineItems = lineItemDao.findByOrderId(orderId);
        
        OrderVO orderVO = new OrderVO(order, lineItems, orderStatus);

        return orderVO;
    }

    public int getNextId(String name) {
        // 根据名称查询序列实体
        Sequence sequence = sequenceDao.findByName(name);

        if (sequence == null) {
            throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name
                    + " sequence).");
        }

        // 获取下一个序列值
        int nextId = sequence.getNextId() + 1;
        sequence.setNextId(nextId);

        // 保存更新后的序列实体
        sequenceDao.save(sequence);

        return nextId;
    }
}
